﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using TouchRC.Classes;

namespace TouchRC.Logic
{
    public static class ListenerLogic
    {
        public static MediaBase ParseReturnValue(string value)
        {
            MediaBase retval = new MediaBase();
            string[] values = value.Split("\r\n".ToCharArray());

            List<string> remianingValues = new List<string>();

            foreach (string content in values)
            {
                if (content.StartsWith("MediaTime"))
                {
                    retval.TotalTrackTime = GetContentValue<TimeSpan>(content);
                }
                else if (content.StartsWith("TrackTime"))
                {
                    retval.CurrentTrackTime = GetContentValue<TimeSpan>(content);
                }
                else if (content.StartsWith("MediaName"))
                {
                    retval.Title = GetContentValue<string>(content);
                }
                else if (content.StartsWith("Play"))
                {
                    retval.PlayState = GetContentValue<bool>(content);
                }
                else
                {
                    remianingValues.Add(content);
                }
            }

            if (value.Contains("TVTuner"))
            {
                retval = new MediaTV(retval);
                retval.Image = ImageFactory.GetMenuOptionImage(MenuItemOption.TvLive);
            }
            else if (value.Contains("PVR="))
            {
                retval = new MediaTV(retval);
                retval.Image = ImageFactory.GetMenuOptionImage(MenuItemOption.TvRecorded);
            }
            else if (value.Contains("StreamingContentVideo"))
            {
                retval = new MediaVideo(retval);
                retval.Image = ImageFactory.GetMenuOptionImage(MenuItemOption.VideoLibrary);
            }
            else if (value.Contains("StreamingContentAudio"))
            {
                MediaAudio mediaAudio = new MediaAudio(retval);
                mediaAudio.Image = ImageFactory.GetMenuOptionImage(MenuItemOption.MusicLibrary);
                foreach (string remainingValue in remianingValues)
                {
                    if (remainingValue.StartsWith("ArtistName"))
                    {
                        mediaAudio.ArtistName = GetContentValue<string>(remainingValue);
                    }
                    else if (remainingValue.StartsWith("TrackName"))
                    {
                        mediaAudio.TrackName = GetContentValue<string>(remainingValue);
                    }
                    else if (remainingValue.StartsWith("TrackDuration"))
                    {
                        mediaAudio.TotalTrackTime = GetContentValue<TimeSpan>(remainingValue);
                    }
                    else if (remainingValue.StartsWith("TotalTracks"))
                    {
                        mediaAudio.TotalTracks = GetContentValue<int>(remainingValue);
                    }
                }
                return mediaAudio;
            }
            else
            {
                NotImplementedException up = new NotImplementedException("Not implemented that yet.");
                throw up; // hehe.
            }

            return retval;
        }

        public static VmcCMetaData ParseMetaDataValue(string value)
        {
            VmcCMetaData retval = new VmcCMetaData();
            string[] values = value.Split(Environment.NewLine.ToCharArray());

            foreach (string content in values)
            {
                if (content.StartsWith("Director"))
                {
                    retval.Director = GetContentValue<string>(content);
                }
                else if (content.StartsWith("Title"))
                {
                    retval.Title = GetContentValue<string>(content);
                }
                else if (content.StartsWith("ChapterTitle"))
                {
                    retval.ChapterTitle = GetContentValue<string>(content);
                }
                else if (content.StartsWith("Duration"))
                {
                    retval.Duration = GetContentValue<TimeSpan>(content);
                }
                else if (content.StartsWith("ReleaseDate"))
                {
                    retval.ReleaseDate = GetContentValue<DateTime>(content);
                }
            }
            return retval;
        }

        private static T GetContentValue<T>(string content)
        {
            int index = content.IndexOf('=');
            string value = content.Substring(index + 1);

            string strType = typeof(T).Name;
            object bob;
            switch (strType)
            {
                case ("TimeSpan"):
                    {
                        if (value.Contains(":"))
                        {
                            TimeSpan newBob = TimeSpan.Parse(value);
                            bob = newBob.Subtract(TimeSpan.FromMilliseconds(newBob.Milliseconds));
                            break;
                        }
                        double parsedValue = double.Parse(value);
                        bob = TimeSpan.FromSeconds(parsedValue);
                        break;
                    }
                case ("DateTime"):
                    {
                        if (!string.IsNullOrEmpty(value))
                        {
                            bob = DateTime.Parse(value);
                        }
                        else
                        {
                            bob = DateTime.MinValue;
                        }
                        break;
                    }
                case ("String"):
                    {
                        bob = value;
                        break;
                    }
                case ("Int32"):
                    {
                        int parsedValue = int.Parse(value);
                        bob = parsedValue;
                        break;
                    }
                case ("Boolean"):
                    {
                        bool parsedValue = bool.Parse(value);
                        bob = parsedValue;
                        break;
                    }
                default:
                    {
                        bob = value;
                        break;
                    }
            }
            return (T)bob;
        }
    }
}
